package com.googlecode.stockhamster.stockmarket;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class AccountStock {
	private String username;
	private int stockID;
	public int amount;

	private Integer ID;// identity

	private static PreparedStatement state = null;
	private static ResultSet rs = null;

	public AccountStock() {
	}

	public AccountStock(String username, int stockID, int amount) {
		this.username = username;
		this.stockID = stockID;
		this.amount = amount;
	}

	public int getStockID() {
		return stockID;
	}

	public int getID() {
		return ID;
	}

	public void setID(Integer ID) {
		this.ID = ID;
	}

	// for database
	public static AccountStock get(String username, int stockID) {
		AccountStock tempAccount = new AccountStock();
		try {
			String sqlGetAccount = "select * from StockAccountStock where username=? and stockID=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccount);
			state.setString(1, username);
			state.setInt(2, stockID);
			rs = state.executeQuery();
			if (!rs.next())
				return null;
			tempAccount = new AccountStock(rs.getString(1), rs.getInt(2), rs
					.getInt(3));
			tempAccount.setID(rs.getInt(4));
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return tempAccount;
	}

	public static AccountStock[] get(String username) {
		AccountStock[] targetList = new AccountStock[0];
		try {
			ArrayList<AccountStock> tempAccount = new ArrayList<AccountStock>();
			String sqlGetAccountList = "select * from StockAccountStock where username=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			state.setString(1, username);
			rs = state.executeQuery();
			while (rs.next()) {
				AccountStock ac = new AccountStock();
				ac = new AccountStock(rs.getString(1), rs.getInt(2), rs
						.getInt(3));
				ac.setID(rs.getInt(4));
				tempAccount.add(ac);
			}
			targetList = new AccountStock[tempAccount.size()];
			for (int i = 0; i < tempAccount.size(); i++)
				targetList[i] = tempAccount.get(i);
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return targetList;
	}

	public static String[] getStockOwner(int stockID) {
		String[] targetList = new String[0];
		try {
			ArrayList<AccountStock> tempAccount = new ArrayList<AccountStock>();
			String sqlGetAccountList = "select * from StockAccountStock where stockID=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			state.setInt(1, stockID);
			rs = state.executeQuery();
			while (rs.next()) {
				AccountStock ac = new AccountStock();
				ac = new AccountStock(rs.getString(1), rs.getInt(2), rs
						.getInt(3));
				ac.setID(rs.getInt(4));
				tempAccount.add(ac);
			}
			targetList = new String[tempAccount.size()];
			for (int i = 0; i < tempAccount.size(); i++)
				targetList[i] = tempAccount.get(i).username;
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return targetList;
	}

	public boolean save() {
		boolean flag = false;
		try {
			String sqlSetStockAccount = "update StockAccountStock set username=?, stockID=?, amount=? where ID=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlSetStockAccount);
			state.setString(1, this.username);
			state.setInt(2, this.stockID);
			state.setInt(3, this.amount);
			state.setInt(4, this.ID);
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
				}
			}
		}
		return flag;
	}

	public boolean insert() {
		boolean flag = false;
		try {
			String sqlAddAmount = "insert into StockAccountStock(username, stockID, amount) values(?,?,?)";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlAddAmount);
			state.setString(1, this.username);
			state.setInt(2, this.stockID);
			state.setInt(3, this.amount);
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
				}
			}
		}
		return flag;
	}

	public boolean delete() {
		boolean flag = false;
		try {
			String sqlDelAccount = "delete from StockAccountStock where ID=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlDelAccount);
			state.setInt(1, this.ID);
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				if (state != null) {
					try {
						state.close();
					} catch (SQLException e) {
					}
				}
			}
		}
		return flag;
	}
}
